package com.example.zg.zg_312_ordering_new.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.zg.zg_312_ordering_new.R;
import com.example.zg.zg_312_ordering_new.adapter.CommentAdapter;
import com.example.zg.zg_312_ordering_new.adapter.FoodInfoAdapter;
import com.example.zg.zg_312_ordering_new.beans.CollectStatus;
import com.example.zg.zg_312_ordering_new.beans.Comment;
import com.example.zg.zg_312_ordering_new.beans.CommentInfo;
import com.example.zg.zg_312_ordering_new.beans.FoodInfo;
import com.example.zg.zg_312_ordering_new.beans.Result;
import com.example.zg.zg_312_ordering_new.common.Constants;
import com.example.zg.zg_312_ordering_new.listener.ListListener;
import com.example.zg.zg_312_ordering_new.listener.RetrofitListener;
import com.example.zg.zg_312_ordering_new.model.CommentModel;
import com.example.zg.zg_312_ordering_new.model.FoodModel;
import com.example.zg.zg_312_ordering_new.model.UserModel;
import com.squareup.picasso.Picasso;

import java.util.List;

public class FoodInfoActivity extends BaseActivity implements RetrofitListener {

    private FoodModel foodModel;
    private UserModel userModel;
    private ImageView foodinfo_return,foodinfo_collect,foodinfo_call;
    private Button foodinfo_purchase;
    private TextView foodinfo_name,foodinfo_intro,foodinfo_price,fooodinfo_bottom_collect;
    private ImageView foodinfo_pic,foodinfo_bottom_img;
    private String phonenum;
    private FoodInfo foodinfo;
    private CommentAdapter adapter;
    private RecyclerView recyclerView;
    private List<Comment> commentList;
    int food_id;
    String result;

    private ListListener listListener = new ListListener() {
        @Override
        public void onResponse(List List) {
            commentList = (List<Comment>) List;
            adapter.setData(commentList);
        }

        @Override
        public void onFail(String msg) {

        }
    };


    @Override
    void initViews() {
        setLayout(R.layout.activity_food_info);

        foodinfo_name=(TextView)findViewById(R.id.textView15);
        foodinfo_intro=(TextView)findViewById(R.id.textView17);
        foodinfo_price=(TextView)findViewById(R.id.textView16);
        foodinfo_pic=(ImageView)findViewById(R.id.imageView2);
        foodinfo_return=findViewById(R.id.imageView3);
        foodinfo_call=findViewById(R.id.imageView14);
        foodinfo_bottom_img=findViewById(R.id.imageView13);
        fooodinfo_bottom_collect=findViewById(R.id.textView20);
        foodinfo_purchase=findViewById(R.id.button3);
        //初始化主体
        recyclerView=findViewById(R.id.recyclerView_food_comment);
    }

    @Override
    void initEvents() {
        foodinfo_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        foodinfo_bottom_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                collect();
            }
        });


        foodinfo_purchase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(FoodInfoActivity.this,PurchaseActivity.class);
                intent.putExtra("id",foodinfo.getShop_id());
                intent.putExtra("foodid",foodinfo.getFood_id());
                intent.putExtra("foodname",foodinfo.getFoodname());
                intent.putExtra("price",foodinfo.getPrice());
                startActivity(intent);
            }
        });
//        foodinfo_call.setOnClickListener(new View.OnClickListener() {
//            @RequiresApi(api = Build.VERSION_CODES.M)
//            @Override
//            public void onClick(View v) {
//                Uri uri=Uri.parse("tel:"+phonenum);
//                Intent intent=new Intent(Intent.ACTION_VIEW,uri);
//                if (checkSelfPermission(Manifest.permission.CALL_PHONE)!=PackageManager.PERMISSION_GRANTED){
//                    startActivity(intent);
//                    return;
//                }
//            }
//        });
    }

    @Override
    void initData() {
        food_id=getIntent().getIntExtra("id",0);
        showFoodInfo(food_id);
        Comment();
        showComments();
        iscollected();
    }
    private void showFoodInfo(int food_id){
        foodModel=new FoodModel();
        foodModel.getFoodById(food_id,this);
    }
    private void showComments(){
        foodModel=new FoodModel();
        foodModel.getAllUserFoodOrder(food_id,this);
    }
    //判断是否收藏
    private void iscollected(){
        foodModel=new FoodModel();
        foodModel.isCollected(getUser_id(),food_id,1,this);
    }
    //收藏/取消收藏
    private void collect(){
        userModel=new UserModel();
        userModel.userCollect(getUser_id(),food_id,this);
    }
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_food_info);
//    }
    private void Comment(){
        CommentModel commentModel = new CommentModel();
        commentModel.getAllComment(food_id,listListener);
        adapter=new CommentAdapter();
        //初始化适配器
        recyclerView.setAdapter(adapter);
        recyclerView.setHasFixedSize(true);    //提高item性能
        recyclerView.addItemDecoration(new DividerItemDecoration(getApplicationContext(),DividerItemDecoration.VERTICAL));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    public void onSuccess(Object o, int flag) {
        switch (flag){
            case Constants.GETFOODBYID:
                  foodinfo=(FoodInfo)o;
                  if (!foodinfo.getPic().equals("")){
                      Picasso.get().load(Constants.BASE_URL+foodinfo.getPic()).into(foodinfo_pic);
                  }
                  foodinfo_intro.setText("简介："+foodinfo.getIntro());
                  foodinfo_price.setText("￥"+foodinfo.getPrice()+"元");
                  foodinfo_name.setText(foodinfo.getFoodname());
                break;
            case Constants.ISCOLLECTED:
                result = ((CollectStatus) o).getCollected().toString();
                if (result.equals("0")){
                    foodinfo_bottom_img.setImageResource(R.drawable.uncollection);
                    fooodinfo_bottom_collect.setText("收藏");

                    fooodinfo_bottom_collect.setTextColor(getResources().getColor(R.color.colorRed));
                }else {
                    foodinfo_bottom_img.setImageResource(R.drawable.collection);
                    fooodinfo_bottom_collect.setText("取消收藏");
                    fooodinfo_bottom_collect.setTextColor(getResources().getColor(R.color.colorBlue));
                }
                break;
            case Constants.USERCOLLECTFOOD:
                if (((Result)o).getSuccess().equals("1")){
                    if (result.equals("0")){
                        foodinfo_bottom_img.setImageResource(R.drawable.collection);
                        fooodinfo_bottom_collect.setText("取消收藏");
                        fooodinfo_bottom_collect.setTextColor(getResources().getColor(R.color.colorRed));
                        showToast("收藏成功");
                        result="1";
                    }else {
                        foodinfo_bottom_img.setImageResource(R.drawable.uncollection);
                        fooodinfo_bottom_collect.setText("收藏");
                        fooodinfo_bottom_collect.setTextColor(getResources().getColor(R.color.colorBlue));
                        showToast("取消收藏成功");
                        result="0";
                    }
                    }else {
                    showToast("操作失败");
                }
                break;
        }
    }

    @Override
    public void onFail() {
        showToast("网络连接错误");
    }
}
